package com.library.controller;

import com.library.bean.ReaderCard;
import com.library.bean.ReaderInfo;
import com.library.service.LoginService;
import com.library.service.ReaderCardService;
import com.library.service.ReaderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
@CrossOrigin
public class ReaderController {

    @Autowired
    private ReaderInfoService readerInfoService;
    @Autowired
    private ReaderCardService readerCardService;
    @Autowired
    private LoginService loginService;

    @RequestMapping("/allreaders.html")
    public ModelAndView allReaders() {
        ArrayList<ReaderInfo> readers = readerInfoService.readerInfos();

        ModelAndView modelAndView = new ModelAndView("admin_readers");
        modelAndView.addObject("readers", readers);
        return modelAndView;

    }

    //    reader_add.html
//    reader_edit.html
//    reader_delete.html
    @RequestMapping("/reader_add.html")
    public ModelAndView readerAdd() {
        ModelAndView modelAndView = new ModelAndView("admin_reader_add");
        return modelAndView;
    }

    @RequestMapping("reader_add_do")
    public String readerInfoAddDo(String name, String sex, String birth, String address, String phone, String password, RedirectAttributes redirectAttributes) {
        ReaderInfo readerInfo = getReaderInfo(0, name, sex, birth, address, phone);

        long readerId = readerInfoService.addReaderInfo(readerInfo);
        readerInfo.setReader_id(readerId);
        if (readerId > 0 && readerCardService.addReaderCard(readerInfo, password)) {
            redirectAttributes.addFlashAttribute("succ", "添加读者信息成功");

        } else redirectAttributes.addFlashAttribute("error", "添加读者信息失败");

        return "redirect:/allreaders.html";
    }

    @RequestMapping("/reader_edit.html")
    public ModelAndView readerinfoedit(HttpServletRequest request){
        long readerId=Long.parseLong(request.getParameter("readerId"));
        ReaderInfo readerInfo=readerInfoService.getReaderInfo(readerId);
        ModelAndView modelAndView=new ModelAndView("admin_reader_edit");
        modelAndView.addObject("readerInfo",readerInfo);
        return modelAndView;

    }

    @RequestMapping("reader_edit_do.html")
    public String readerInfoEditDo(HttpServletRequest request,String name, String sex, String birth, String address, String phone,RedirectAttributes redirectAttributes){
        long readerId=Long.parseLong(request.getParameter("readerId"));
        ReaderInfo readerInfo = getReaderInfo(readerId, name, sex, birth, address, phone);

        if (readerId > 0 && readerCardService.editReaderCard(readerInfo)&&readerInfoService.editReaderInfo(readerInfo)) {
            redirectAttributes.addFlashAttribute("succ", "修改读者信息成功");

        } else redirectAttributes.addFlashAttribute("error", "修改读者信息失败");

        return "redirect:/allreaders.html";


    }
    @RequestMapping("reader_edit_do_r.html")
    public String readerInfoEditDOreader(HttpServletRequest request,String name, String sex, String birth, String address, String phone,RedirectAttributes redirectAttributes){
        ReaderCard readerCard= (ReaderCard) request.getSession().getAttribute("readercard");
        ReaderInfo readerInfo=getReaderInfo(readerCard.getReader_id(), name, sex, birth, address, phone);
        if (readerInfoService.editReaderInfo(readerInfo)){
            ReaderCard readerCardNew=loginService.findReaderCardByReaderId(readerCard.getUsername());
            request.getSession().setAttribute("readercard",readerCardNew);
            redirectAttributes.addFlashAttribute("succ","修改读者信息成功");

        }else{
            redirectAttributes.addFlashAttribute("error","修改读者信息失败");
        }
        return "redirect:/reader_info_show.html";

    }

    @RequestMapping("/reader_info_show.html")
    public ModelAndView toReaderInfoShow(HttpServletRequest request){
        ReaderCard readerCard= (ReaderCard) request.getSession().getAttribute("readercard");
        ReaderInfo readerInfo=readerInfoService.getReaderInfo(readerCard.getReader_id());

        ModelAndView modelAndView=new ModelAndView("reader_info");
        modelAndView.addObject("readerInfo",readerInfo);
        return modelAndView;

    }




    @RequestMapping("reader_delete.html")
    public String readerDelete(HttpServletRequest request,RedirectAttributes redirectAttributes){
        long readerId=Long.parseLong(request.getParameter("readerId"));
        if (readerInfoService.deleteReaderInfo(readerId)&&readerCardService.deleteReaderCard(readerId)){
            redirectAttributes.addFlashAttribute("succ","删除读者信息成功");
        }else{
            redirectAttributes.addFlashAttribute("error","删除读者信息失败");
        }
        return "redirect:/allreaders.html";
        }


    private ReaderInfo getReaderInfo(long readerId,String name, String sex, String birth, String address, String phone) {
        ReaderInfo readerInfo=new ReaderInfo();
        Date date=new Date();

        try {

            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            date=df.parse(birth);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        readerInfo.setAddress(address);
        readerInfo.setReader_id(readerId);
        readerInfo.setBirth(birth);
        readerInfo.setName(name);
        readerInfo.setPhone(phone);
        readerInfo.setSex(sex);
        return readerInfo;
    }

    @RequestMapping("/reader_info.html")
    public ModelAndView ReaderInfo(HttpServletRequest request){
        ReaderCard readerCard= (ReaderCard) request.getSession().getAttribute("readercard");
        ReaderInfo readerInfo=readerInfoService.getReaderInfo(readerCard.getReader_id());


        ModelAndView modelAndView=new ModelAndView("reader_info_edit");
        modelAndView.addObject("readerinfo",readerInfo);
        return modelAndView;
    }



}



